package prictice.visits;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * @Package prictice.visits
 * @Description:
 * @Author elwyn
 * @Date 2017/9/14 23:48
 * @Email elonyong@163.com
 */
public class PVBolt1 implements IRichBolt {
    OutputCollector outputCollector;

    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        this.outputCollector = outputCollector;
    }

    private String logString = null;
    private String sessionId = null;
    long pv;

    @Override
    public void execute(Tuple tuple) {
        logString = tuple.getString(0);
        sessionId = logString.split("\t")[1];
        if (sessionId != null) {
            pv++;
        }
        outputCollector.emit(new Values(Thread.currentThread().getId(), pv));
        System.err.println(Thread.currentThread().getId() + " pv = " + pv);
    }

    @Override
    public void cleanup() {

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("threadId","pv"));
    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }
}
